/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin_conexion;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Chapas Esta Clase hará las conexiones directas a la base de datos
 *
 */
public class AdminConexion {

    private Connection conn;

    public AdminConexion() {
        conn = null;
    }

    
    public boolean update(String sentence) throws InstantiationException, ClassNotFoundException, IllegalAccessException, SQLException {

        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "tecpro";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "pass123";
      
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, userName, password);
            System.out.println("Connected to the database");

            Statement st = conn.createStatement();

            st.executeUpdate(sentence);

            conn.close();
            System.out.println("Update successful");
            return true;
       
            
       

    }

    public boolean Query(String sentence) {

        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "AutoVentas";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "ventasaut";
        String password = "pass123";
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, userName, password);
            System.out.println("Connected to the database");

            Statement st = conn.createStatement();

            st.executeQuery(sentence);
            conn.close();
            System.out.println("Query Successful");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public Connection abrirConexionBDD() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
        conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "tecpro";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "pass123";
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url + dbName, userName, password);
        return conn;
    }

    public int cerrarConexionBDD() throws SQLException {
        conn.close();
        return 0;
    }

   
    public ResultSet consulta(String query) throws SQLException {
        ResultSet rs;
        Statement st = conn.createStatement();        
        rs = st.executeQuery( query );
        return rs;
    }
    
   
}
